﻿using UnityEngine;

public class Player : MonoBehaviour
{
    private Rigidbody2D rb;
    private float h;
    private float v;

    public PlayerDataSO playerData;
    private PlayerWeapon weapon;
    public void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        weapon = transform.Find("PlayerWeapon").GetComponent<PlayerWeapon>();
        InputMgr.GetInstance().StartOrEndCheck(true);
        EventCenter.GetInstance().AddEventListener<KeyCode>("某键按下", OnKeyDown);
        EventCenter.GetInstance().AddEventListener<KeyCode>("某键抬起", OnKeyUp);
    }

    public void OnKeyDown(KeyCode e)
    {
       switch(e)
       {
            case KeyCode.W:
                v += 1;
                break;
            case KeyCode.A:
                h -= 1;
                break;
            case KeyCode.S:
                v-= 1;
                break; 
            case KeyCode.D:
                h += 1;
                break;
        }
    }

    public void OnKeyUp(KeyCode e)
    {
        switch (e)
        {
            case KeyCode.W:
                v -= 1;
                break;
            case KeyCode.A:
                h += 1;
                break;
            case KeyCode.S:
                v += 1;
                break;
            case KeyCode.D:
                h -= 1;
                break;
        }
    }
    /// <summary>
    /// 玩家面朝方法
    /// </summary>
    public void Follow()
    {
        Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        if (pos.x > transform.position.x)
        {
            transform.localScale = new Vector3(1,1,1);
        }
        else
        {
            transform.localScale = new Vector3(-1,1,1);
        }
        weapon.Follow(pos);
    }
    private void FixedUpdate()
    {
        rb.velocity = new Vector2(h, v) * playerData.moveSpeed;

        Follow();
    }
}